function main() {
	var cts = chrome.extension.getBackgroundPage().settings.getContacts();
	for ( var i in cts) {
		$(newRow(cts[i])).insertBefore("#newEditor");
	}

	$("#addButton").click(function() {
		var name = $("#name").val();
		var email = $("#email").val();

		var newContact = {
			"name" : name,
			"email" : email
		};

		var row = newRow(newContact);
		$(row).insertBefore("#newEditor");
		$(row).fadeTo("slow", 0.0, function() {
			$(this).fadeTo("slow", 1.0);
		});

		cts.push(newContact);
		chrome.extension.getBackgroundPage().settings.save();

		$("#name").val("");
		$("#email").val("");
		$("#newEditor").hide();
	});

	$("#list-wrap").addClass("show");
	$("#newEditor").hide();
	$("#new-button").click(function() {
		$("#newEditor").fadeIn();
		$("#name").focus();
	});

	$("#email").keydown(function(e) {
		if (e.keyCode == 13) {
			$("#addButton").click();
		}
	});
}

function newRow(c) {
	var newRow = document.createElement("tr");
	newRow.contact = c;

	$(newRow).html(
			"<td class=\"handle\">x</td><td class='name-cell'>" + c.name
					+ "</td><td class='email-cell'>" + c.email
					+ "</td><td>&nbsp;</td>").addClass("entry");

	$(".handle", newRow).css("cursor", "pointer").click(function() {
		erase(newRow);
	});

	return newRow;
}

function erase(row) {
	cts = chrome.extension.getBackgroundPage().settings.getContacts();
	var find = -1;
	for ( var i in cts) {
		var next = cts[i];
		if (next.name === row.contact.name && next.email === row.contact.email) {
			find = i;
			break;
		}
	}
	if (find >= 0) {
		cts.splice(find, 1);
		chrome.extension.getBackgroundPage().settings.save();
	}
	$(row).fadeOut();
}